﻿/*=====================================================================
	TASK 13:                               { Operators and Expressions}
	Write a program that exchanges bits 3, 4 and 5 with bits 24, 25 and 
    26 of given 32-bit unsigned integer. 
======================================================================*/

using System;

class Exchange345bitsWith242526
{
    static void Main()
    {
        Console.Write("Enter number n=");
        uint n = uint.Parse(Console.ReadLine());

        Console.WriteLine("n in binary is:{0}\n", Convert.ToString(n, 2).PadLeft(32, '0'));

        for (int p = 3; p < 6; p++)
        {
            uint startMask = 1u << p;
            uint nAndstartMask = n & startMask;
            uint bitPosition345 = nAndstartMask >> p;
            Console.WriteLine("bit at position {0} is:{1}",p,bitPosition345);

            uint endmask = 1u << (p + 21);
            uint nAndendMask = n & endmask;
            uint bitPosition242526 = nAndendMask >> (p+21);
            Console.WriteLine("bit at position {0} is:{1}", p+21, bitPosition242526);
            
            if (bitPosition345 != bitPosition242526)
            {
                Console.WriteLine("n in binary before exchange is:{0}", Convert.ToString(n, 2).PadLeft(32, '0'));
                n = n ^ startMask;
                n = n ^ endmask;
                Console.WriteLine("n in binary after exchange  is:{0}\n", Convert.ToString(n, 2).PadLeft(32, '0'));
            }

            else
            {
                Console.WriteLine("bit{0}=bit{1}={2} ,n doesn't exchange:{3}\n", p, p + 21, bitPosition345, Convert.ToString(n, 2).PadLeft(32, '0'));
            }
        }

        Console.WriteLine("n after all exchanges is:{0}", Convert.ToString(n, 2).PadLeft(32, '0'));
    }
}

